import { _decorator, Camera, CCFloat, CCInteger, Component, director, gfx, ImageAsset, Material, Node, renderer, RenderTexture, Sprite, SpriteFrame, Texture2D } from 'cc';
const { ccclass, property, executeInEditMode } = _decorator;
/**
 * 残影效果实现
 */
@ccclass('Ghost')
// @executeInEditMode
export class Ghost extends Component {
    @property({ type: Node })
    mainCameraNode: Node = null!;
    @property({ type: Node })
    ghostCameraNode: Node = null!;
    @property
    _ghostNum: number = 5;
    @property({ type: CCInteger })
    public get ghostNum() {
        return this._ghostNum;
    };
    @property
    _ghostInterval: number = 0.1;
    @property({ type: CCFloat })
    public get ghostInterval() {
        return this._ghostInterval;
    };
    public set ghostInterval(val) {
        this._ghostInterval = val;
    };
    @property({ type: Sprite })
    ghostSprite: Sprite = null;
    @property({ type: Material })
    ghostMaterial: Material = null!;

    private _mainCamera: Camera = null!;
    private _ghostCamera: Camera = null!;
    private _renderTexture: RenderTexture = null;

    private _ghostTexures: Array<Texture2D> = null;
    private _ghostIntervalDt: number = 0;

    onLoad(): void {
        if (!this.ghostCameraNode || !this.mainCameraNode) return;
        this._mainCamera = this.mainCameraNode.getComponent(Camera);
        this._ghostCamera = this.ghostCameraNode.getComponent(Camera);
        this._ghostTexures = new Array();
        this._ghostIntervalDt = 10;
        this.initRenderTexture();
    }
    private initRenderTexture(): void {
        this._renderTexture = new RenderTexture();
        this._renderTexture.reset({
            width: this._mainCamera.camera.width,
            height: this._mainCamera.camera.height,
        });
        this._ghostCamera.targetTexture = this._renderTexture;
        let spriteFrame: SpriteFrame = new SpriteFrame();
        spriteFrame.texture = this._renderTexture;
        this.ghostSprite.spriteFrame = spriteFrame;
        console.log("初始化rt", this._renderTexture);
    }
    start() {

    }

    update(deltaTime: number) {
        this.updateCameraTransform();
        this.updateGhostTexures(deltaTime);
    }
    private updateCameraTransform(): void {
        if (!this.ghostCameraNode || !this.mainCameraNode) return;
        this.ghostCameraNode.worldPosition = this.mainCameraNode.worldPosition;
        this.ghostCameraNode.worldScale = this.mainCameraNode.worldScale;
        this.ghostCameraNode.worldRotation = this.mainCameraNode.worldRotation;
    }
    private updateGhostTexures(deltaTime: number): void {
        this._ghostIntervalDt += deltaTime;
        if (this._ghostIntervalDt >= this.ghostInterval) {
            this._ghostIntervalDt = 0;
        } else {
            return;
        }
        if (!this.ghostCameraNode || !this.mainCameraNode) return;
        if (this._ghostTexures.length >= this._ghostNum) {
            // 删除最后一个
            let texture: Texture2D = this._ghostTexures.splice(this._ghostNum - 1, 1)[0];
            texture.destroy();
        }

        let textureBuffer: Uint8Array = this._renderTexture.readPixels();
        //通过texture buffer 创建texture 
        let img = new ImageAsset();
        img.reset({
            _data: textureBuffer,
            width: this._renderTexture.width,
            height: this._renderTexture.height,
            format: Texture2D.PixelFormat.RGBA8888,
            _compressed: false
        });
        let texture: Texture2D = new Texture2D();
        texture.image = img;
        this._ghostTexures.unshift(texture);

        for (let i = 0; i < this._ghostNum; i++) {
            this.ghostMaterial.setProperty("ghostTexure_" + i, this._ghostTexures[i]);
        }

    }

}


